<template>
  <!-- 侧边栏容器 -->
  <div class="sidebar-container">
    <!-- 折叠/展开按钮 -->
    <button class="toggle-btn" @click="toggleSidebar">
      {{ isSidebarOpen ? '←' : '→' }}
    </button>

    <!-- 侧边栏主体 -->
    <nav class="sidebar" :class="{ 'sidebar-closed': !isSidebarOpen }">
      <!-- Logo区域 -->
      <div class="logo">
        <span v-if="isSidebarOpen">商品销售平台</span>
        <span v-else>商</span>
      </div>

      <!-- 导航链接区域 -->
      <div class="nav-links">
        <router-link
            to="/products"
            class="link"
            :class="{ 'active': $route.path === '/products' }"
        >
          <span class="icon">📦</span>
          <span v-if="isSidebarOpen">商品列表</span>
        </router-link>
        
        <router-link
            to="/invitations"
            class="link"
            :class="{ 'active': $route.path === '/invitations' }"
        >
          <span class="icon">💬</span>
          <span v-if="isSidebarOpen">帖子列表</span>
        </router-link>
        
        <!-- 登录后才显示的功能 -->
        <template v-if="isLoggedIn">
          <router-link
              to="/create-product"
              class="link"
              :class="{ 'active': $route.path === '/create-product' }"
          >
            <span class="icon">➕</span>
            <span v-if="isSidebarOpen">创建商品</span>
          </router-link>
          
          <router-link
              to="/create-invitation"
              class="link"
              :class="{ 'active': $route.path === '/create-invitation' }"
          >
            <span class="icon">✏️</span>
            <span v-if="isSidebarOpen">发帖子</span>
          </router-link>
        </template>
      </div>

      <!-- 用户操作区域 -->
      <div class="user-actions">
        <button v-if="isLoggedIn" @click="logout" class="btn">
          <span class="icon">🚪</span>
          <span v-if="isSidebarOpen">退出登录</span>
        </button>
        <router-link v-else to="/login" class="btn">
          <span class="icon">🔑</span>
          <span v-if="isSidebarOpen">登录/注册</span>
        </router-link>
      </div>
    </nav>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue';
import { useRouter, useRoute } from 'vue-router';

// 新增：侧边栏展开状态
const isSidebarOpen = ref(true);
const router = useRouter();
const route = useRoute();
const isLoggedIn = ref(false);

// 新增：切换侧边栏展开/折叠
const toggleSidebar = () => {
  isSidebarOpen.value = !isSidebarOpen.value;
};

// 检查登录状态的函数
const checkLoginStatus = () => {
  isLoggedIn.value = localStorage.getItem('isLogin') === 'true';
};

// 原有逻辑保留（登录状态检查）
onMounted(() => {
  checkLoginStatus();

  // 监听窗口大小自动调整侧边栏
  const handleResize = () => {
    if (window.innerWidth < 768) {
      isSidebarOpen.value = false;
    } else {
      isSidebarOpen.value = true;
    }
  };

  window.addEventListener('resize', handleResize);
  handleResize(); // 初始化
  
  // 监听localStorage变化，实时更新登录状态
  window.addEventListener('storage', checkLoginStatus);
});

// 监听路由变化，实时检查登录状态
watch(route, () => {
  checkLoginStatus();
});

const logout = () => {
  localStorage.removeItem('isLogin');
  localStorage.removeItem('userInfo');
  checkLoginStatus(); // 立即更新状态
  router.push('/products'); // 退出后跳转到商品列表
};
</script>

<style scoped>
/* 容器样式 */
.sidebar-container {
  position: relative;
  height: 10vh;
}

/* 折叠/展开按钮 */
.toggle-btn {
  position: fixed;
  left: 240px;
  top: 20px;
  z-index: 100;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: none;
  background: #4CAF50;
  color: white;
  cursor: pointer;
  box-shadow: 0 2px 5px rgba(0,0,0,0.2);
  transition: left 0.3s ease;
}

/* 侧边栏主体样式 */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  width: 240px;
  background-color: #333;
  color: white;
  display: flex;
  flex-direction: column;
  padding: 20px 0;
  transition: width 0.3s ease, left 0.3s ease;
  z-index: 99;
}

/* 折叠状态样式 */
.sidebar-closed {
  width: 60px;
}

.sidebar-closed .toggle-btn {
  left: 60px;
}

/* Logo样式 */
.logo {
  padding: 0 20px 20px;
  font-size: 1.2rem;
  font-weight: bold;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  text-align: center;
}

/* 导航链接区域 */
.nav-links {
  flex: 1;
  padding-top: 20px;
}

/* 链接样式 */
.link {
  display: flex;
  align-items: center;
  padding: 12px 20px;
  color: white;
  text-decoration: none;
  transition: background-color 0.2s;
}

.link:hover, .link.active {
  background-color: rgba(255,255,255,0.1);
}

.link .icon {
  margin-right: 10px;
  font-size: 1.2rem;
}

/* 用户操作区域 */
.user-actions {
  padding: 20px;
  border-top: 1px solid rgba(255,255,255,0.1);
}

.btn {
  width: 100%;
  padding: 10px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  background-color: #4CAF50;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn .icon {
  margin-right: 8px;
}

.btn:hover {
  background-color: #45a049;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .sidebar {
    left: -240px;
  }

  .sidebar-closed {
    left: 0;
    width: 60px;
  }

  .toggle-btn {
    left: 20px;
  }
}
</style>